package com.tencent.sr.iris.basic.saas.service.asynctask.operate;

import cn.hutool.core.collection.CollUtil;
import com.tencent.sr.iris.activity.interfaces.request.operate.OperateBoardThirdRequest;
import com.tencent.sr.iris.activity.interfaces.response.group.operate.OperateThirdBoardResponse;
import com.tencent.sr.iris.baisc.saas.export.sdk.BaseExportTask;
import com.tencent.sr.iris.baisc.saas.export.sdk.ScrollRequest;
import com.tencent.sr.iris.baisc.saas.export.sdk.ScrollResponse;
import com.tencent.sr.iris.baisc.saas.export.sdk.TaskContextHolder;
import com.tencent.sr.iris.basic.saas.service.dependency.OperateBoardDependency;
import com.tencent.sr.iris.basic.saas.service.model.operate.OperateThirdRankExportInfo;
import com.tencent.sr.rmall.common.request.PaginationRequest;
import com.tencent.sr.rmall.common.response.PaginationResponse;
import com.tencent.sr.rmall.material.interfaces.enums.ETaskBusinessType;
import com.tencent.sr.rmall.material.interfaces.response.TaskRecordResponse;
import com.tencent.sr.rmall.springbootstarter.elasticjob.TaskJob;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@TaskJob(jobName = "iris:export:OperateThirdRankExportTask",cron = "0/5 * * * * ? ",shardingTotalCount = 1, description = "导出")
@Component
@Slf4j
public class OperateThirdRankExportTask extends
        BaseExportTask<OperateBoardThirdRequest, OperateThirdRankExportInfo> {
    private static ThreadLocal<AtomicInteger> lineNum = ThreadLocal.withInitial(AtomicInteger::new);
    private final int maxLines = 20000;
    private final int pageSize = 50;

    @Resource
    private OperateBoardDependency operateBoardDependency;

    @Override
    public ScrollResponse<OperateThirdRankExportInfo> get(ScrollRequest<OperateBoardThirdRequest> scrollRequest) {
        TaskRecordResponse taskRecord = TaskContextHolder.get();
        log.info("OperateThirdRankExportTask: {}, request: {}", taskRecord, scrollRequest);
        ScrollResponse<OperateThirdRankExportInfo> result = new ScrollResponse<>();
        // 判断是否已经达到文件行数上限
        AtomicInteger num = lineNum.get();
        if (num.get() >= maxLines) {
            lineNum.set(new AtomicInteger());
            return result;
        }
        OperateBoardThirdRequest queryCriteria = Optional.ofNullable(scrollRequest.getQueryCriteria())
                .orElseGet(OperateBoardThirdRequest::new);
        PaginationRequest<OperateBoardThirdRequest> paginationRequest = new PaginationRequest<>();
        paginationRequest.setQueryCriteria(queryCriteria);
        int pageNum = (num.get()) / pageSize + 1;
        paginationRequest.setPageNum(pageNum);
        paginationRequest.setPageSize(pageSize);

        PaginationResponse<OperateThirdBoardResponse> response =
                operateBoardDependency.queryOperateThirdBoard(paginationRequest);
        log.info("OperateThirdRankExportTask response:{}", response);

        List<OperateThirdRankExportInfo> exportInfoList = new ArrayList<>();
        if (CollUtil.isNotEmpty(response.getDataList())) {
            for (OperateThirdBoardResponse boardResponse : response.getDataList()) {
                OperateThirdRankExportInfo operateThirdRankExportInfo = new OperateThirdRankExportInfo();
                // 数据转换
                operateThirdRankExportInfo.readFromSearchResponseData(boardResponse);
                // 设置行号
                operateThirdRankExportInfo.setNum(num.incrementAndGet());
                exportInfoList.add(operateThirdRankExportInfo);
                // 最多支持5000条商品数据导出
                if (num.get() >= maxLines) {
                    break;
                }
            }
            result.setDataList(exportInfoList);
            // 最后一页
            if (response.getDataList().size() < pageSize) {
                num.addAndGet(pageSize - response.getDataList().size());
            }
        } else {
            lineNum.set(new AtomicInteger());
        }
        result.setScrollId(String.valueOf(System.currentTimeMillis()) + exportInfoList.size());
        log.info("OperateThirdRankExportTask result:{}", result);
        return result;
    }

    @Override
    public String exportType() {
        return ETaskBusinessType.IRIS_OPERATE_RANK_THIRD.name();
    }
}
